package com.interview.ujiuye;

import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer1 {

	public static void main(String[] args) throws Exception {
		System.out.println("服务器启动成功,等待连接......");
		ServerSocket serverSocket = new ServerSocket(8888);
		//等待连接, accept接受的意思,当运行到该方法,出现线程阻塞
		//服务器通过该Socket对象和客户端进行通信,  ServerSocket只接受新连接
		Socket socket = serverSocket.accept();
		//获取连接的IP地址
		String addr = socket.getInetAddress().getHostAddress();
		System.out.println(addr+"连接到服务器....小伙子你跑不了");
		Thread.sleep(Integer.MAX_VALUE);
		//获取输出流
		OutputStream os = socket.getOutputStream();
		//获取字符串的字节数组
		byte[] bytes = "元旦快乐".getBytes();
		//向输出流写字节数组
		os.write(bytes);
		//关闭连接
		os.close();
		serverSocket.close();
	}
}